#面向对象（万物皆对象）的三大特征：封装，继承，多态
#小项目：封装 ->入口函数main 自定义函数 全局变量
import threading
import urllib3
from bs4 import BeautifulSoup

target_url='https://p.weather.com.cn/zrds/index.shtml'

header={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}


urls = [] #存储图片链接的列表
threads = []#初始化一个列表用于存储所以的线程对象

# 解析网页数据
def parseData(data):
    count = 0
    soup = BeautifulSoup(data,"lxml")
    images = soup.select("img") #返回img图片列表
    for img in images:
        try:...
        except Exception as err:
            print(err)


#下载图片的操作
def download(url,count):
    try:
        if(url[len(url)-4] =="."):
            ext  = url[len(url)-4:]
        else:
            ext = ".png"
        req =  urllib3.PoolManager().request('GET',url,headers=header)
        data = req.data
        img_file = open("images\\" + str(count) + ext,"wb")
        img_file.write(data)
        img_file.close()
        print("图片已下载：" + str(count) + ext)
    except Exception as err:
          print(err)

def requestData():
    # 使用urllib3进行数据请求
    http = urllib3.PoolManager()
    rq = http.request('GET', target_url, headers=header)
    # 查看服务器的响应
    # print("获得网页返回的内容：", rq.date.decode('utf-8'))
    return rq.data.decode('utf-8')

if  __name__ == '__main__':
    data = requestData()
    parseData(data)






























